package com.binhthuan.catechist.server.service;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.binhthuan.catechist.server.ResponseObject;
import com.binhthuan.catechist.server.bus.ParentBUS;
import com.binhthuan.catechist.server.domain.Parent;
import com.binhthuan.catechist.shared.constant.Action;
import com.binhthuan.catechist.shared.constant.Parameter;

public class ParentServlet extends AbstractServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

    }

    @Override
    protected void fetch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        if (request.getParameter(Parameter.ID) == null) {
            getAllParents(request, response);
        } else {
            getParent(request, response);
        }
    }

    @Override
    protected void remove(HttpServletRequest request, HttpServletResponse response, boolean confirmation) throws ServletException,
            IOException {
        // TODO Auto-generated method stub

    }

    @Override
    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        // TODO Auto-generated method stub

    }

    private void getAllParents(HttpServletRequest request, HttpServletResponse response) throws IOException {
        try {
            int startRow = Integer.parseInt(request.getParameter(Action.Information.START_ROW));
            int endRow = Integer.parseInt(request.getParameter(Action.Information.END_ROW));
            String sortByString = request.getParameter(Action.Information.SORT_BY);

            boolean sortASC = checkSortASC(sortByString);
            String sortByColumn = (sortByString == null) ? Parameter.FIRST_NAME : getSortingColumn(sortByString);

            ParentBUS bus = new ParentBUS();
            List<Parent> shoolDays = bus.getAllParents(startRow, endRow - startRow, sortByColumn, sortASC);

            sendResponse(new ResponseObject(shoolDays).toJson());
        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        }
    }

    private void getParent(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String parentId = request.getParameter(Parameter.ID);

        try {
            int id = Integer.parseInt(parentId);

            ParentBUS bus = new ParentBUS();
            Parent parent = bus.getParentById(id);

            sendResponse(new ResponseObject(parent).toJson());
        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
        }
    }
}
